package com.ys.service.amazonorder.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.amazonorder.TbBaseinfoSpcz;
import com.ys.mapper.amazonorder.TbBaseinfoSpczMapper;
import com.ys.mapper.business.DiySqlMapper;
import com.ys.service.amazonorder.TbBaseinfoSpczService;
import com.ys.vo.R;

@Service("tbBaseinfoSpczService")
public class TbBaseinfoSpczServiceImpl implements TbBaseinfoSpczService {
	
	@Autowired
	private TbBaseinfoSpczMapper tbBaseinfoSpczMapper;
	@Autowired
	private DiySqlMapper diySqlMapper;
	@Override
	public R add(Map<String, Object> params) {

		TbBaseinfoSpcz entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoSpcz.class);

		String s = "^.{1,20}$";
		if(!entity.getMaterialCn().matches(s)){
			return R.error("输入有误,请重新输入");
		}
		String m = "^.{1,50}$";
		if(!entity.getMaterialEn().matches(m)){
			return R.error("输入有误,请重新输入");
		}
		if (new TbBaseinfoSpcz().setMaterialCn(entity.getMaterialCn()).count() > 0) {
			return R.error("该数据已存在");
		}
		if (new TbBaseinfoSpcz().setMaterialCn(entity.getMaterialCn()).count() > 0) {
			return R.error("该数据已存在");
		}
		tbBaseinfoSpczMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	public R edit(Map<String, Object> params) {
		


		TbBaseinfoSpcz entity = JSON.parseObject(JSON.toJSONString(params), TbBaseinfoSpcz.class);

		String s = "^.{1,20}$";
		if(!entity.getMaterialCn().matches(s)){
			return R.error("输入有误,请重新输入");
		}
		String m = "^.{1,50}$";
		if(!entity.getMaterialEn().matches(m)){
			return R.error("输入有误,请重新输入");
		}

		tbBaseinfoSpczMapper.update(entity);
		return R.ok(EditDto.closeAndReload());
	}

	/**
	 * 删除
	 * @operateCompanyId operateCompanyId
	 * @return
	 */
	@Override
	public R deleteTbBaseinfoSpcz(Long id) throws Exception{
		diySqlMapper.deleteTbBaseinfoSpcz(id);
		return R.ok(EditDto.closeAndReload());


	}


}
